<?php
/**
* @package   auto_occasion
* @subpackage formule
* @author    CMS
* @copyright 2011
* @license    All rights reserved
*/

class BoFormuleCtrl extends jController {

public $pluginParams = array(
                                    '*'=>array('auth.required'=>true,
                                    'jacl2.right'=>'admin.compte.gerer'
                                    )
							);

    /**
    *Fonction qui affiche la liste des formules 
    */
    function index() {

        jClasses::inc("formule~formuleSrv");

        $offset        =  $this->intParam('offset',0,true);
        $nbitemparpage = FORMULE_NBITEMPARPAGE;
        $liste = formuleSrv::liste($offset, $nbitemparpage);
        $option_formule = formuleSrv::getOptionFormuleList();

        $properties = array('start-label' => '',
                              'prev-label'  => '<',
                              'next-label'  => '>',
                              'end-label'   => '',
                              'area-size'   => 1);

        $listeAll = formuleSrv::getAll($liste['formule'], $option_formule);

        $rep = $this->getResponse('html');
        $rep->menusActifs = adminHtmlResponse::MENU_TARIFS ;

        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/jquery-ui.css');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/demos.css');
        
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.ui.core.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.ui.widget.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery-ui.min.js');

        $rep->body->assign('liste',$listeAll);
        $rep->body->assign('recordCount',$liste['nbRecords']);
        $rep->body->assign('nbitemparpage', $nbitemparpage);
        $rep->body->assign('page', $offset);
        $rep->body->assign('properties', $properties);

        $rep->bodyTpl = 'formule~BoFormuleListe';

        return $rep;
    }


    /**
     * Ajout / Edition d'une formule
     *
     *
     */
    function editer() {

        $rep = $this->getResponse('html');
        $rep->menusActifs = adminHtmlResponse::MENU_TARIFS ;
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/jquery-ui.css');
      
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.min.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery-ui.min.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'ckeditor_3.6.3/ckeditor/ckeditor.js');

        jClasses::inc("formule~formuleSrv");
        jClasses::inc("formule~formuleOptionSrv");

        $toOptions      = formuleSrv::liste_option();
        $id             = $this->intParam('id', 0);
        $formule        = ($id) ? formuleSrv::chargeParId($id) : formuleSrv::init();
        $option_formule = ($id) ? formuleSrv::getOptionFormule($id) : array();
        
        $ArrayTmp = array() ;
        foreach ($toOptions as $oOption) {
            $oOption->toChoises = formuleOptionSrv::getChoiseByOptionId($oOption->id_option_formule) ;
            $ArrayTmp[] = $oOption ;
        }

        $toOptions  = $ArrayTmp ;
        $tabOpt     = array();
        // récupère les choix correspondant à la formule % à la table : lien_optionformule
        if ($id) {
            $opt    = formuleSrv::getOptionPresente($id);
            $tabOpt = ($opt) ? explode(",", $opt) : array();
            $rep->body->assign('tabOpt', $tabOpt);
        }

        $rep->body->assign('id',      $id);
        $rep->body->assign('formule', $formule);
        $rep->body->assign('toOptions', $toOptions);
        //print_r($toOptions);die();
        $rep->body->assign('option_formule', $option_formule);

        $rep->bodyTpl = 'formule~BoFormuleEdit';

        return $rep;
    }


    /** 
     * Sauvegarde d'un formule
     * Redirection vers page liste formule après sauvegarde 
     */
     function sauvegarder() {
        
        jClasses::inc("formule~formuleSrv");

        $zIdOpt = $this->stringParam('choix-associated', '');
        $zOption             = implode('', $this->param('parution')) ;

        $id                  = $this->intParam('id','');
        $formule             = jDao::createRecord('commun~ao_formule') ;
        $formule->titre      = $this->stringParam('titre','');
        $formule->content    = $this->param('content');
        $formule->prix       = $this->stringParam('prix','');
        $formule->status     = $this->stringParam('status','');
        $formule->debut      = $this->stringParam('debut',''); 
        $formule->fin        = $this->stringParam('fin','');
        $formule->options    = $zOption;
        $formule->id         =  $id;
        
        if ($id){
             $formule = formuleSrv::modifier($formule);
             formuleSrv::addIdOpt($id, $zIdOpt);
        } else {
             $formule = formuleSrv::ajouter($formule);
             formuleSrv::addIdOpt($formule->id, $zIdOpt);
        }

        $rep                 = $this->getResponse('redirect');
        $rep->action         = 'formule~BoFormule:index';

        return $rep;
    }
    
    function supprimer () {

        jClasses::inc("formule~formuleSrv");

        $id  = $this->intParam('id') ;
        FormuleSrv::supprimer($id) ;

        $rep = $this->getResponse('redirect') ;
        $rep->action = 'formule~BoFormule:index' ;

        return $rep;
        
    }
   

}

